//index.js
//获取应用实例
const app = getApp();
const util = require('../../utils/util');
const common = require('../../utils/common');
const requestUtil = require('../../utils/request');
const api = require('../../config/api');

Page({
  data: {
	TabCur:0,
	user_info:"",
	info_true:false,
	// 列表titel
	list_titel:[{
		index:0,
		name:"企业介绍"
	},
	{
		index:1,
		name:"企业视频"
	},
	{
		index:2,
		name:"企业文化"
	},
	{
		index:3,
		name:"为你推荐"
	},
	{
		index:4,
		name:"联系我们"
	}],
	tuijian_info:[],
	cpt_info:{}
  },

   onShow: async function () {  
	   this.get_cpt_info()
  },
  onLoad() {
	  this.setData({
		  user_info:wx.getStorageSync("info")
	  })
  },
  
  scrollToElement(id) {
    let contentId = '#';
	if(id==0){
		contentId+='zoor'
	}else if(id==1){
		contentId+='one'
	}else if(id==2){
		contentId+='two'
	}else if(id==3){
		contentId+='three'
	}else if(id==4){
		contentId+='four'
	}else{
		wx.pageScrollTo({
		  scrollTop: 0,
		  duration: 300
		});
		return false;
	}
    wx.createSelectorQuery().select(contentId).boundingClientRect(rect => {
        if (rect) {
          wx.pageScrollTo({
            scrollTop: rect.top,
            duration: 300
          });
        } else {
          console.log('元素仍未找到');
        }
      })
      .exec();
  },
  tabSelect(e){
	  if(e){
		  this.setData({
		  		  TabCur:e.target.dataset.id
		  })
	  }
	  setTimeout(()=>{
		  this.scrollToElement(e.target.dataset.id)
	  },300)
	

  },
  canParseAsJson(jsonString) {
      try {
          JSON.parse(jsonString);
          return true; // 如果没有抛出异常，说明可以被解析为JSON
      } catch (e) {
          return false; // 如果抛出异常，说明不是有效的JSON
      }
  },
  callup(){
	  console.log("点击了拨打电话按钮");
	  wx.makePhoneCall({
	    phoneNumber: this.data.cpt_info.cpPhone //仅为示例，并非真实的电话号码
	  })
  },
  // 查找电子名片下的  公司简介信息
  get_cpt_info(){
	  requestUtil.request("POST","/cpt_sql/getCpt",{
		  cpId:this.data.user_info.pcId
	  }).then((res)=>{
		  if(res.data.status==200){
			  res.data.data[0].cpTitelImg=this.canParseAsJson(res.data.data[0].cpTitelImg)?JSON.parse(res.data.data[0].cpTitelImg):res.data.data[0].cpTitelImg
			  
			  res.data.data[0].cpVideo=this.canParseAsJson(res.data.data[0].cpVideo)?JSON.parse(res.data.data[0].cpVideo):res.data.data[0].cpVideo
			let text_wenhua=res.data.data[0].cpCulture.replace("；", ";")
			 let data= text_wenhua.split(";")
			 let data_wenhua=[]
			 for (let i = 0; i < data.length; i++) {
				 if(data[i].length!=0){
					 let a=data[i].replace("，", ",")
					 data_wenhua.push({titel:a.split(",")[0],text:a.split(",")[1]})
				 }
			 }
			 res.data.data[0].cpCulture=data_wenhua
			this.setData({
				cpt_info:res.data.data[0]
			})
			this.setData({
				info_true:true
			})
			this.tuijian_info_get()
		  }
	  })
  },
  tuijian_info_get(){
	  requestUtil.request("POST","/cpt_sql/cpt_tuijian",{
		  cpType:this.data.cpt_info.cpType
	  }).then((res)=>{
		  if(res.data.status==200){
			  let data=[]
			  let list=res.data.data.list
			  for (let i = 0; i < list.length; i++) {
				  if(this.data.cpt_info.cpId!=list[i].cpId){
					  list[i].cpXcxm=JSON.parse(list[i].cpXcxm)
					 data.push(list[i]) 
				  }
			  }
			  this.setData({
				  tuijian_info:data
			  })
		  }
	  })
  }

})